package com.yuecai.modules.search;

import com.yuecai.modules.yc_brand.domain.YcBrand;
import com.yuecai.modules.yc_brand.service.YcBrandService;
import com.yuecai.modules.yc_goods.domain.YcGoods;
import com.yuecai.modules.yc_goods.service.YcGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private YcGoodsService ycGoodsService;
    @Autowired
    private YcBrandService ycBrandService;


    @GetMapping("/{name}")
    public HashMap<Integer,Object> searchMessage(@PathVariable String name){
        name = "%"+name+"%";
        List<YcGoods> gods = ycGoodsService.findByGoodName(name);

        HashMap<Integer, Object> map = new HashMap<>();
        map.put(1,gods);

        List<YcBrand> ycBrands = ycBrandService.findByName(name);
        map.put(2,ycBrands);

        return map;
    }


    /**
     * 测试全局异常
     * @throws Exception
     */
    @GetMapping("/")
    public void searchMessage() throws Exception {

        try {
            int inn = 100/0;
        }catch (Exception e){
            throw new Exception("等会拉三等奖法撒旦吧！");
        }
    }
}
